package ar.edu.uba.fi.jmit.graphvisualizer.server.storage;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;
import ar.edu.uba.fi.jmit.graphvisualizer.server.ResourceProperties;
import ar.edu.uba.fi.jmit.graphvisualizer.server.service.MethodTimeEventServiceImpl;

public class MethodTimeEventServiceImplTestCase extends TestCase {
	
	public void test() {
		MethodTimeEventServiceImpl service = new MethodTimeEventServiceImpl();
		Registry.getMonitoringDataStorage().addData(this.createMap(10, 10, "MethodTimeEventServiceImplTestCase.test", "testMonitor"));
		assertTrue(service.getMonitorData("MethodTimeEventServiceImplTestCase.test", "testMonitor").size() == 1);
		assertTrue(service.getMonitorData("anotherApplication", "testMonitor").size() == 0);
		assertTrue(service.getMonitorData("MethodTimeEventServiceImplTestCase.test", "anotherMonitor").size() == 0);
		assertTrue(service.getAverageData("MethodTimeEventServiceImplTestCase.test", "testMonitor").size() == 1);
		assertEquals(new Integer(10), service.getMonitorData("MethodTimeEventServiceImplTestCase.test", "testMonitor").get(0).getValue());
		assertEquals(new Integer(10), service.getAverageData("MethodTimeEventServiceImplTestCase.test", "testMonitor").get(0).getValue());
	}
	
	public void test2() {
		MethodTimeEventServiceImpl service = new MethodTimeEventServiceImpl();
		Registry.getMonitoringDataStorage().addData(this.createMap(10, 10, "MethodTimeEventServiceImplTestCase.test2", "testMonitor"));
		assertTrue(service.getMonitorData("MethodTimeEventServiceImplTestCase.test2", "testMonitor").size() == 1);
		assertTrue(service.getMonitorData("anotherApplication", "testMonitor").size() == 0);
		assertTrue(service.getMonitorData("MethodTimeEventServiceImplTestCase.test2", "anotherMonitor").size() == 0);
		assertTrue(service.getAverageData("MethodTimeEventServiceImplTestCase.test2", "testMonitor").size() == 1);
		assertEquals(new Integer(10), service.getMonitorData("MethodTimeEventServiceImplTestCase.test2", "testMonitor").get(0).getValue());
		assertEquals(new Integer(10), service.getAverageData("MethodTimeEventServiceImplTestCase.test2", "testMonitor").get(0).getValue());
		Registry.getMonitoringDataStorage().addData(this.createMap(2, 6, "MethodTimeEventServiceImplTestCase.test2", "testMonitor"));
		Registry.getMonitoringDataStorage().addData(this.createMap(3, 7, "MethodTimeEventServiceImplTestCase.test2", "testMonitor"));
		Registry.getMonitoringDataStorage().addData(this.createMap(9, 10, "MethodTimeEventServiceImplTestCase.test2", "testMonitor"));
		assertTrue(service.getMonitorData("MethodTimeEventServiceImplTestCase.test2", "testMonitor").size() == 4);
		assertTrue(service.getAverageData("MethodTimeEventServiceImplTestCase.test2", "testMonitor").size() == 4);
		
	}

	private Map<String, Serializable> createMap(int time, int avg, String applicationName, String monitorName) {
		Map<String, Serializable> map = new HashMap<String, Serializable>();
		map.put(ResourceProperties.getProperty("jmit.graphvisualizer.methodTime.elapsedTime"), time);
		map.put(ResourceProperties.getProperty("jmit.graphvisualizer.methodTime.average"), avg);
		map.put(ResourceProperties.getProperty("jmit.graphvisualizer.monitor.monitorType"), "METHOD_TIME");
		map.put(ResourceProperties.getProperty("jmit.graphvisualizer.key.date"), new Date());
		map.put(ResourceProperties.getProperty("jmit.graphvisualizer.monitor.clientApplicationName"), applicationName);
		map.put(ResourceProperties.getProperty("jmit.graphvisualizer.key.monitorName"), monitorName);
		return map;
	}

}
